﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using TestSystem.DataManipulation;
using TestSystem.Ui.Helpers;

namespace TestSystem.Ui.Forms
{
    public class GetBarForm : QuoteForm
    {
        private DateTime datetime;
        private Decimal high;
        private Decimal open;
        private Decimal low;
        private Decimal close;
        private int volume;
        public Quote Quote { get; private set; }
        private  string beginvalues = "0" + DecimalHelper.GetDecimalSeparator() + "0000";
        public GetBarForm()
        {
            InitializeComponent();
            DateTimePicker.Tag = true;
            OpenValue.Tag = false;
            HighValue.Tag = false;
            LowValue.Tag = false;
            CloseValue.Tag = false;
            VolumeValue.Tag = false;

        }

        #region Initialization
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // DateTimePicker
            // 
            this.DateTimePicker.Validating += new System.ComponentModel.CancelEventHandler(this.DateTimePickerValidating);
            this.DateTimePicker.Validated += new System.EventHandler(this.OnValidated);
            // 
            // OpenValue
            // 
            this.OpenValue.Validating += new System.ComponentModel.CancelEventHandler(this.OnValidating);
            this.OpenValue.Validated += new System.EventHandler(this.OnValidated);
            // 
            // HighValue
            // 
            this.HighValue.Validating += new System.ComponentModel.CancelEventHandler(this.OnValidating);
            this.HighValue.Validating +=new CancelEventHandler(this.HighValueValidating);
            this.HighValue.Validated += new System.EventHandler(this.OnValidated);
            // 
            // LowValue
            // 
            this.LowValue.Validating += new System.ComponentModel.CancelEventHandler(this.OnValidating);
            this.LowValue.Validating+=new CancelEventHandler(this.LowValueValidating);
            this.LowValue.Validated += new System.EventHandler(this.OnValidated);
            // 
            // CloseValue
            // 
            this.CloseValue.Validating += new System.ComponentModel.CancelEventHandler(this.OnValidating);
            this.CloseValue.Validated += new System.EventHandler(this.OnValidated);
            // 
            // VolumeValue
            // 
            this.VolumeValue.Validating += new System.ComponentModel.CancelEventHandler(this.VolumeValueValidating);
            this.VolumeValue.Validated += new System.EventHandler(this.OnValidated);
            // 
            // CancelBtn
            // 
            this.CancelBtn.Click += new System.EventHandler(this.CancelBtn_Click);
            // 
            //
            //OkBtn
            //
            this.OkBtn.Click +=new EventHandler(this.OkBtnClick);
            // GetBarForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.ClientSize = new System.Drawing.Size(404, 182);
            this.Name = "GetBarForm";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        protected override void OnLoad(EventArgs e)
        {
            Text = @"Bar";
            DateTimePicker.CustomFormat = @"MM.dd.yyyy hh:mm:ss";
            OpenValue.Text = beginvalues;
            HighValue.Text = beginvalues;
            LowValue.Text = beginvalues;
            CloseValue.Text = beginvalues;
            OkBtn.Enabled = false;
        }
        #endregion

        #region Validating

        private void VolumeValueValidating(object sender, CancelEventArgs e)
        {
            var digitbox = (TextBox)sender;
            Int32 currentvalue;
            if ((digitbox.Text != string.Empty) && (Int32.TryParse(digitbox.Text, NumberStyles.Any,
                 CultureInfo.GetCultureInfo(("en-US")), out currentvalue)))
                digitbox.Tag = currentvalue > 0;
            else digitbox.Tag = false;
        }

        private void DateTimePickerValidating(object sender, CancelEventArgs e)
        {
            DateTimePicker.Tag = DateTimePicker.Value <= DateTime.Now;
        }

        private void LowValueValidating(object sender, CancelEventArgs e)
        {
            if (!CheckNullValues()) return;
            var minvalue = new[] { open, high, low, close }.Min();
            LowValue.Tag = low == minvalue;
        }

        private void HighValueValidating(object sender, CancelEventArgs e)
        {
            if (!CheckNullValues()) return;
            var maxvalue = new[] { open, high, low, close }.Max();
            HighValue.Tag = high == maxvalue;
        }
        private void OnValidating(object sender, CancelEventArgs e)
        {
            var digitbox = (TextBox)sender;
            SetValues();
            Decimal currentvalue;
            if ((digitbox.Text != string.Empty) && (Decimal.TryParse(digitbox.Text, NumberStyles.Any,
                 CultureInfo.GetCultureInfo(("en-US")), out currentvalue)))
                digitbox.Tag = currentvalue > 0;
            else digitbox.Tag = false;
        }
        private void OnValidated(object sender, EventArgs e)
        {
            OkBtn.Enabled = AreAllValuesCorrect();
        }
        private bool AreAllValuesCorrect()
        {
            return (Boolean)OpenValue.Tag && (Boolean)HighValue.Tag && (Boolean)LowValue.Tag && (Boolean)CloseValue.Tag &&
                   (Boolean)VolumeValue.Tag && (Boolean)DateTimePicker.Tag;
        }
        #endregion
     
        private void OkBtnClick(object sender, EventArgs e)
        {
           
            datetime = DateTimePicker.Value;
            Quote = new Quote(datetime, open, high, low, close, volume);
            DialogResult = DialogResult.OK;

            Close();
        }

        private Boolean CheckNullValues()
        {
            return open != 0 && high != 0 && low != 0 && close != 0;
        }
      
        private void SetValues ()
        {

            Decimal.TryParse(OpenValue.Text, NumberStyles.Any, CultureInfo.GetCultureInfo(("en-US")),
                             out open);
            Decimal.TryParse(HighValue.Text, NumberStyles.Any, CultureInfo.GetCultureInfo(("en-US")),
                             out high);
            Decimal.TryParse(LowValue.Text, NumberStyles.Any, CultureInfo.GetCultureInfo(("en-US")),
                             out low);
            Decimal.TryParse(CloseValue.Text, NumberStyles.Any, CultureInfo.GetCultureInfo(("en-US")),
                             out close);
            Int32.TryParse(OpenValue.Text, NumberStyles.Any, CultureInfo.GetCultureInfo(("en-US")),
                             out volume);

        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}
